package com.guanghua.brick.html.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.IDataSource;
import com.guanghua.brick.html.WebControl;

public class CoreElseIfTag extends BodyTagSupport {
	
private static Log logger = LogFactory.getLog(CoreElseTag.class);
	
	private String id = null;
	private Boolean test = true;
	private String testSource = null;
	
	public int doStartTag() throws JspException {
		
		Boolean t = (Boolean)pageContext.getRequest().getAttribute(this.id);
		if (t == null) {
			logger.warn("elseif control must have the same id's if control");
			return BodyTagSupport.SKIP_BODY;
		} else if (t) {
			return BodyTagSupport.SKIP_BODY;
		}
		
		if (this.test == null && this.testSource != null) {
			IDataSource ds = WebControl.getDataSourceParser(pageContext);
			this.test = (Boolean) ds.parseDataSource(pageContext, this.testSource, Boolean.class);
		}
		
		pageContext.getRequest().setAttribute(this.id, this.test);
		if (this.test) {
			return BodyTagSupport.EVAL_BODY_INCLUDE;
		} else {
			return BodyTagSupport.SKIP_BODY;
		}
	}

	public Boolean getTest() {
		return test;
	}

	public void setTest(Boolean test) {
		this.test = test;
	}

	public String getTestSource() {
		return testSource;
	}

	public void setTestSource(String testSource) {
		this.testSource = testSource;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
